package com.niit.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.niit.service.AdminService;
import com.niit.model.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
/**
 * @Author: 王治伟
 *
 */
@CrossOrigin(origins = {"http://localhost:63343","http://localhost","http://127.0.0.1","http://localhost/","http://127.0.0.1/"},maxAge = 3600)
@Api(value = "管理员模块",tags = {"管理员模块"})
@RestController
public class AdminController {
    @Autowired
    private AdminService adminService;

    /**
     * 服务熔断，对查询管理员列表的备选方案
     * @param
     * @return
     */
    public List<Admin> getByIdDown(){
        //服务器压力过大
        Admin admin1 = new Admin();
        admin1.setName("服务熔断,服务器发生了异常").setPassword("");
        List<Admin> admins = new ArrayList<>();
        admins.add(admin1);
        return admins;
    }

    @ApiOperation("获得管理员列表")
    @GetMapping("/admin")
    @HystrixCommand(defaultFallback = "getByIdDown")
    public List<Admin> adminlist(){ return  adminService.getadminList(); }
    @ApiOperation("获得学生列表")
    @GetMapping("/student")
    public List<Student> studentlist(){
        return  adminService.getstudentList();
    }
    @ApiOperation("获得教师列表")
    @GetMapping("/teacher")
    public List<Teacher> teacherlist(){ return adminService.getteacherList() ; }
    @ApiOperation("获得班级列表")
    @GetMapping("/class")
    public List<Classes> classlist(){
        return adminService.getclassList();
    }
    @ApiOperation("获得题目列表")
    @GetMapping("/question")
    public List<Question> questionslist(){
        return adminService.getquestionList();
    }
    @ApiOperation("获得试卷列表")
    @GetMapping("/exampaper")
    public List<ExamPaper> examPaperslist(){
        return adminService.getexampaperList();
    }


    @ApiOperation("根据id获得管理员信息")
    @GetMapping("/admin/{id}")
    public Admin getadminById(@PathVariable("id") Long id){ return adminService.selectAdminId(id); }
    @ApiOperation("根据id获得学生信息")
    @GetMapping("/student/{id}")
    public Student getstudentById(@PathVariable("id") Long id){ return adminService.selectStudentId(id); }
    @ApiOperation("根据id获得老师信息")
    @GetMapping("/teacher/{id}")
    public Teacher getteacherById(@PathVariable("id") Long id){ return adminService.selectTeacherId(id); }
    @ApiOperation("根据id获得班级信息")
    @GetMapping("/class/{id}")
    public Classes getclassById(@PathVariable("id") Long id){ return adminService.selectClassId(id); }
    @ApiOperation("根据id获得题目信息")
    @GetMapping("/question/{id}")
    public Question getquestionById(@PathVariable("id") Long id){ return adminService.selectQuestionId(id); }
    @ApiOperation("根据id获得试卷信息")
    @GetMapping("/exampaper/{id}")
    public ExamPaper getExamPaperById(@PathVariable("id") Long id){ return adminService.selectExamPaperId(id); }

    @ApiOperation("添加管理员")
    @PostMapping("/admin")
    public String add(@RequestBody Admin admin){
        return adminService.addAdmin(admin) >0?"success":"fail";
    }
    @ApiOperation("添加学生")
    @PostMapping("/student")
    public String add(@RequestBody Student student){
        return adminService.addStudent(student)>0?"success":"fail";
    }
    @ApiOperation("添加老师")
    @PostMapping("/teacher")
    public String add(@RequestBody Teacher teacher){
        return adminService.addTeacher(teacher)>0?"success":"fail";
    }
    @ApiOperation("添加班级")
    @PostMapping("/class")
    public String add(@RequestBody Classes classes){
        return adminService.addClass(classes)>0?"success":"fail";
    }
    @ApiOperation("添加题目")
    @PostMapping("/question")
    public String add(@RequestBody Question question){
        return adminService.addQuestion(question)>0?"success":"fail";
    }
    @ApiOperation("添加试卷")
    @PostMapping("/exampaper")
    public String add(@RequestBody ExamPaper examPaper){ return adminService.addExamPaper(examPaper)>0?"success":"fail"; }

    @ApiOperation("更新管理员信息")
    @PutMapping("/admin")
    public String updateadmin(@RequestBody Long id){ return adminService.upAdmin(id)>0?"success":"fail"; }
    @ApiOperation("更新老师信息")
    @PutMapping("/teacher")
    public String updateteacher(@RequestBody Long id){ return adminService.upTeacher(id)>0?"success":"fail"; }
    @ApiOperation("更新学生信息")
    @PutMapping("/student")
    public String updatestudent(@RequestBody Long id){ return adminService.upStudent(id)>0?"success":"fail"; }
    @ApiOperation("更新班级信息")
    @PutMapping("/class")
    public String updateclass(@RequestBody Long id ){ return adminService.upClass(id)>0?"success":"fail"; }
    @ApiOperation("更新题目信息")
    @PutMapping("/question")
    public String updatequestion(@RequestBody  Long id){ return adminService.upQuestion(id)>0?"success":"fail"; }
    @ApiOperation("更新试卷信息")
    @PutMapping("/exampaper")
    public String updateexampaper(@RequestBody   Long id){ return adminService.upExamPaper(id)>0?"success":"fail";}
    @ApiOperation("更新学生状态")
    @PutMapping("/status")
    public String updatestatus(@RequestBody  Long id){ return adminService.upstatus(id)>0?"success":"fail";}


    @ApiOperation("根据id删除管理员")
    @DeleteMapping("/admin/{id}")
    public String deleteAdminById(@PathVariable("id") Long id){ return  adminService.deleteAdminById(id)>0?"success":"fail"; }
    @ApiOperation("根据id删除学生")
    @DeleteMapping("/student/{id}")
    public String deleteStudentById(@PathVariable("id") Long id){ return  adminService.deleteStudentById(id)>0?"success":"fail"; }
    @ApiOperation("根据id删除老师")
    @DeleteMapping("/teacher/{id}")
    public String deleteTeacherById(@PathVariable("id") Long id){ return  adminService.deleteTeacherById(id)>0?"success":"fail"; }
    @ApiOperation("根据id删除班级")
    @DeleteMapping("/class/{id}")
    public String deleteClassById(@PathVariable("id") Long id){ return  adminService.deleteClassById(id)>0?"success":"fail"; }
    @ApiOperation("根据id删除题目")
    @DeleteMapping("/question/{id}")
    public String deleteQuestionById(@PathVariable("id") Long id){ return  adminService.deleteQuestionById(id)>0?"success":"fail"; }
    @ApiOperation("根据id删除试卷")
    @DeleteMapping("/exampaper/{id}")
    public String deleteExamPaperById(@PathVariable("id") Long id){ return  adminService.deleteExamPaperById(id)>0?"success":"fail"; }
}
